<?php

class admingearController extends base
{
    
    public function __construct()
    {
        parent::__construct('admin');
        // $this->checkPrivilege(common::request('a', 'R'));       
        $this->load('admin');
    }
    /**
    *
    *添加,修改装备集合
    */
    public function doaddSet(){
    	if($_POST){
    		$succ=true;
    		$err=array();    	
    		$setId= common::request('id','P');
        	($setName = common::request('name', 'P'))||$succ=false||$err[]='集合名必填';       	 	
       		$isShow =(int) common::request('isShow', 'P');       		
       	    ($setDescribe = common::request('describe','P'))||$succ=false||$err[]='描述必填';
       	    $logo= common::request('logo','P');
       	    $img= common::request('img','P');
       	    //如果是新注册并且集合名存在
       	    if(!$setId&&$this->db->getRow("select * from r_gear_sets where setName='{$setName}'")){
       	    	$succ=false;
       	    	$err[]='集合已存在';
       	    }
    		$newLogo = $this->load('qiniu')->uploadQiNiuImg('newLogo', md5(time().common::getHashStr(6)));
    		if($newLogo)
    		{ 
    			$logo=$newLogo ;
    		}
    			else if(!$logo)    		
    		{ 
    			$succ=false;
    			$err[]='请上传logo';
    		}	
    		$newImg = $this->load('qiniu')->uploadQiNiuImg('newImg', md5(time().common::getHashStr(6)));
    		if($newImg)
    		{ 
    			$img=$newImg ;
    		}
    		else if (!$img)    	
    		{ 
    			$succ=false;
    			$err[]='请上传背景图';
    		}	
    		$addTime=time();    			
    		if($succ){
    			if($setId)
    			{
    				//修改
    				$setInfo = compact('setId','setName','setDescribe','logo','img','addTime','isShow');
    				$this->db->insertRowTable('r_gear_set',$setInfo);
    				echo "<script>alert('修改成功');location.href='/?c=admingear&a=setList'</script>";
    			}
    			else
    			{
    				//新增
    				$setInfo = compact('setName','setDescribe','logo','img','addTime','isShow'); 
    				 $newId = $this->db->insertRowTable('r_gear_sets',$setInfo);
    				// //初始化3个redis，最新，最热，精华
    			    $redisKeyBase=REDIS_PREFIX.'gear_';    				
    				$this->redis->zadd($redisKeyBase.'new',$addTime,'s:'.$newId);
    				 //集合初始化有20的热度和精华
    				$this->redis->zadd($redisKeyBase.'hot',20,'s:'.$newId);
    				$this->redis->zadd($redisKeyBase.'best',20,'s:'.$newId);
    				//集合单独排序，冗余字段，方便搜索
    				$this->redis->zadd($redisKeyBase.'hot_s',20,$newId);
    				$this->redis->zadd($redisKeyBase.'best_s',20,$newId);


    				echo "<script>alert('添加成功');location.href='/?c=admingear&a=setList'</script>";
    			}
    		}else{
    			$this->view->assign('err',$err);
    		}
    	}
    	$id=common::request('id','G',0);
    	if($id)
    	{
    		$setInfo=$this->db->getRow("select * from r_gear_sets where setId =$id");
    		if(empty($setInfo))
    		{
    			exit('非法访问');
    		}
    	}
    	$setInfo =isset($setInfo)?$setInfo :array('setId' => 0, 'setName' => '', 'logo' => '', 'img'=>'','setDescribe' => '', 'isShow' => 1);
        $this->view->assign('setInfo', $setInfo);
    	$this->view->display('gear/addset');
    }
    /**
    *
    *没必要分页
    *集合列表
    */
    public function dosetList(){
    	$list=$this->db->getAll('select * from r_gear_sets');
    	$redisKeyBase=REDIS_PREFIX.'gear_'; 
    	foreach($list as &$v){
    		$v['addTime']=date('Y-m-d H:i:s',$v['addTime']);
    		$v['hot']=$this->redis->zscore($redisKeyBase.'hot','s:'.$v['setId']);
    		$v['best']=$this->redis->zscore($redisKeyBase.'best','s:'.$v['setId']);    		
    	}
		$this->view->assign('list', $list);
    	$this->view->display('gear/setList');
    }
    /**
    *
    *处理集合加热减热，加精减精
    */
    public function doaddsub(){
    	if(!common::is_ajax()){exit ('非法访问');}
    	$num=(int)common::request('num','P');
    	$type=common::request('type','P');
    	$id=common::request('id','P');
    	$redisKeyBase=REDIS_PREFIX.'gear_';
    	$return=$this->redis->zincrby($redisKeyBase.$type,$num,'s:'.$id);
    	$this->redis->zincrby($redisKeyBase.$type.'_s',$num,$id);
    	echo $return;
    }
     /**
    *
    *处理装备加热减热，加精减精
    */
    public function dogearaddsub(){
    	if(!common::is_ajax()){exit ('非法访问');}
    	$num=(int)common::request('num','P');
    	$type=common::request('type','P');
    	$id=common::request('id','P');
    	$redisKeyBase=REDIS_PREFIX.'gear_';
    	$return=$this->redis->zincrby($redisKeyBase.$type,$num,'g:'.$id);
    	$this->redis->zincrby($redisKeyBase.$type.'_g',$num,$id);
    	echo $return;
    }
      /**
    *
    *处理帖子加热减热，加精减精
    */
    public function donewsaddsub(){
    	if(!common::is_ajax()){exit ('非法访问');}
    	$num=(int)common::request('num','P');
    	$type=common::request('type','P');
    	$id=common::request('id','P');
    	$redisKeyBase=REDIS_PREFIX.'gear_';
    	$return=$this->redis->zincrby($redisKeyBase.$type,$num,'n:'.$id);
    	$this->redis->zincrby($redisKeyBase.$type.'_n',$num,$id);
    	echo $return;
    }
    /**
    *
    *
    *集合旗下装备
    */
    public function dosetGear(){
    	$setId=common::request('id','G');
    	if(!$this->db->getRow("select * from r_gear_sets where setId={$setId}")){exit("非法访问");}
    	$list=$this->db->getAll("select * from r_gear where isShow=1 and gearId in (select gid from r_gear_gs where sid={$setId} )");
        foreach ($list as & $v) {
                $v = $this->load('gear')->getGearInfo($v['gearId']);              
        }
        $this->view->assign('setId', $setId);
    	$this->view->assign('list', $list);
    	$this->view->assign('pageStr','');
    	$this->view->display('gear/setGear');
    }
    /**
    *
    *从集合中删除装备
    */
    public function dodelfromset(){
    	if(!common::is_ajax()){exit ('非法访问');}
    	$gid=common::request('gid','P');
    	$sid=common::request('sid','P');    	
    	echo  $this->db->update("delete from r_gear_gs where gid={$gid} and sid={$sid}");    	
	}
	/**
	*@from /?c=admin&a=gearList
	*添加到集合中前，拉列表
	*/
	public function doaddtosetbefore(){
		if(!common::is_ajax()){exit ('非法访问');}
    	$gid=common::request('gid','P');    	
    	$list=$this->db->getAll("select setId,setName from r_gear_sets where setId not in (select sid from r_gear_gs where gid={$gid})");
    	echo json_encode($list);
	}
	/**
	*@from /?c=admin&a=gearList
	*添加到集合中
	*/
	public function doaddtoset(){
		if(!common::is_ajax()){exit ('非法访问');}
    	$gid=common::request('gid','P');
    	$sid=common::request('sid','P');
    	//正常操作不会进这里
    	//echo "select  * from r_gear_gs where gid={$gid} and sid={$sid}";exit;
    	if($this->db->getOne("select * from  r_gear_gs where gid={$gid} and sid={$sid}")){
    		$ret['status']=1;
    		$ret['msg']='装备已在集合里';
    		exit(json_encode($ret));
    	}
    	$info=compact('gid','sid');    	
    	$this->db->insertRowTable('r_gear_gs',$info);
    	$ret['status']=0;
    	$ret['msg']='添加成功';
    	echo json_encode($ret);    	
	}	
}